package com.cgbl.service.vm

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.WorkServiceFactory
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.model.TeamListBean
import com.robot.base.util.LiveDataEventManager
import com.robot.base.util.ToastUtils
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import com.robot.base.model.BaseResponse
import com.robot.base.model.MemberAdapterBean

class AddTeamViewModel(application: Application) : GLBaseViewModel(application) {

    var teamName = ObservableField<String>()

    var teamDesc = ObservableField<String>()

    val uc = UIChangeEvent()

    var id = ""

    var teamMemberIds = mutableListOf<String>()

    val submit = BindingCommand<Any>(BindingAction {
        updateTeamProfile()
    })

    class UIChangeEvent {

        val teamMemberEvent = SingleLiveEvent<MutableList<MemberAdapterBean>>()

    }

    fun fetchTeamProfile(id: String) {
        this.id = id
        if (TextUtils.isEmpty(id)) return
        WorkServiceFactory.getTeamById(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<TeamListBean>>(this) {
                override fun onNext(t: BaseResponse<TeamListBean>) {
                    uc.teamMemberEvent.postValue(t.data.accountDOS as MutableList<MemberAdapterBean>?)
                    teamName.set(t.data.teamName)
                    teamDesc.set(t.data.teamInfo)
                }
            })
    }

    private fun updateTeamProfile() {
        val teamNameParams = teamName.get() ?: ""
        val teamInfoParams = teamDesc.get() ?: ""
        if (TextUtils.isEmpty(teamNameParams)) {
            ToastUtils.showShort("请输入团队名称!")
            return
        }
        val params = mapOf(
            "id" to id,
            "teamAccountIds" to teamMemberIds,
            "teamInfo" to teamInfoParams,
            "teamName" to teamNameParams,
        )
        if (TextUtils.isEmpty(id)) {
            WorkServiceFactory.createTeam(params)
                .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                    override fun onNext(t: BaseResponse<String>) {
                        LiveDataEventManager.postRefreshTeamEvent()
                        finish()
                    }
                })
        } else {
            WorkServiceFactory.updateTeam(params)
                .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                    override fun onNext(t: BaseResponse<String>) {
                        LiveDataEventManager.postRefreshTeamEvent()
                        finish()
                    }
                })
        }

    }
}